import "reflect-metadata";

export function createParamDecorator(keyOrFactory: string | Function) {
  return (data?: any) => (target: any, propertyKey: string, parameterIndex: number) => {
    const existingParamters = Reflect.getMetadata(`param`, target, propertyKey) || [];
    if (typeof keyOrFactory === "string") {
      existingParamters[parameterIndex] = { parameterIndex, key: keyOrFactory, data };
      // console.log(existingParamters);
    } else {
      existingParamters[parameterIndex] = { parameterIndex, key: "DecoratorFatcory", data, factory: keyOrFactory };
    }

    Reflect.defineMetadata(`param`, existingParamters, target, propertyKey);
  };
}

export const Request = createParamDecorator("Request");
export const Req = createParamDecorator("Req");
export const Query = createParamDecorator("Query");
export const Headers = createParamDecorator("Headers");
export const Body = createParamDecorator("Body");
export const Response = createParamDecorator("Response");
export const Next = createParamDecorator("Next");
